//
//  MPBidTableViewContainer.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/6.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPBidTableViewContainer.h"
#import "MPBidDefaultTableController.h"
#import "MPBidDurationTableController.h"
#import "MPBidIncomeTableController.h"
#import "MPBidPlatformTableController.h"
#import "MPBidProgressTableController.h"
#import "RDVTabBarController.h"
#import "MZTimerLabel.h"
#import "UIButton+Biz.h"
#import "MPPlatformFilterTableController.h"
#import "MPNavigationViewController.h"

@interface MPBidTableViewContainer () <MZTimerLabelDelegate>
@property (nonatomic, strong)MZTimerLabel* timerLabel;
@property (nonatomic, strong)UILabel* timerShowLabel;
@property (nonatomic, assign)eMPVisitorSubscribeType visitType;

@end

@implementation MPBidTableViewContainer

- (instancetype)initWithVisitType:(eMPVisitorSubscribeType)type
{
    if (self = [super init]) {
        self.visitType = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我要抢标";
    
    // navBar
    UIButton* filterNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [filterNavBtn setImage:[UIImage imageForKey:@"nav_filter_normal"] withTitle:@"筛选" font:[UIFont systemFontOfSize:kTitleFontSize] forState:UIControlStateNormal];
    [filterNavBtn setImage:[UIImage imageForKey:@"nav_filter_focus"] withTitle:@"筛选" font:[UIFont systemFontOfSize:kTitleFontSize] forState:UIControlStateHighlighted];
    [filterNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [filterNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    [filterNavBtn addTarget:self action:@selector(filterNavBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:filterNavBtn];

    NSMutableArray *pages = [NSMutableArray array];
    MPBidDefaultTableController* defaultBid = [[MPBidDefaultTableController alloc] initWithVisitType:self.visitType];
    defaultBid.viewTitle = @"默认";
    defaultBid.delegate = self;
    
    MPBidPlatformTableController* platformBid = [[MPBidPlatformTableController alloc] init];
    platformBid.viewTitle = @"平台";
    platformBid.delegate = self;

    MPBidIncomeTableController* incomeBid = [[MPBidIncomeTableController alloc] init];
    incomeBid.viewTitle = @"收益";
    incomeBid.delegate = self;

    MPBidDurationTableController* durationBid = [[MPBidDurationTableController alloc] init];
    durationBid.viewTitle = @"期限";
    durationBid.delegate = self;

    MPBidProgressTableController* progressBid = [[MPBidProgressTableController alloc] init];
    progressBid.viewTitle = @"进度";
    progressBid.delegate = self;

    [pages addObject:defaultBid];
    [pages addObject:platformBid];
    [pages addObject:incomeBid];
    [pages addObject:durationBid];
    [pages addObject:progressBid];
    [self setPages:pages];
    
    self.pageControl.sortArrowImageUp = [UIImage imageForKey:@"segment_icon_arrow_up"];
    self.pageControl.sortArrowImageDown = [UIImage imageForKey:@"segment_icon_arrow_down"];
    
    [self.view addSubview:self.timerShowLabel];
    
    [self.timerShowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pageControl.mas_bottom);
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(@35.0f);
    }];
    
    [self.contentContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timerShowLabel.mas_bottom);
        make.leading.bottom.trailing.mas_equalTo(self.view);
    }];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.timerLabel pause];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.timerLabel reset];
    [self.timerLabel start];
}

- (void)filterNavBtnPressed:(id)sender
{
    KDLogTrace();
    MPPlatformFilterTableController* vc = [[MPPlatformFilterTableController alloc] initWithStyle:UITableViewStyleGrouped];
    MPNavigationViewController* nav = [[MPNavigationViewController alloc] initWithRootViewController:vc];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.rdv_tabBarController.tabBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - property
- (MZTimerLabel *)timerLabel
{
    if (!_timerLabel)
    {
        _timerLabel = [[MZTimerLabel alloc] initWithLabel:self.timerShowLabel andTimerType:MZTimerLabelTypeTimer];
        [_timerLabel setCountDownTime:20.0f];
        _timerLabel.delegate = self;
    }
    return _timerLabel;
}

- (UILabel *)timerShowLabel
{
    if (!_timerShowLabel) {
        _timerShowLabel = [[UILabel alloc] init];
        _timerShowLabel.numberOfLines = 1;
        _timerShowLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _timerShowLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _timerShowLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _timerShowLabel;
}


-(NSString*)timerLabel:(MZTimerLabel*)timerLabel customTextToDisplayAtTime:(NSTimeInterval)time
{
    if (time <= 0.1) {
        return @"正在刷新";
    }
    return [NSString stringWithFormat:@"抢标进行中，距下一次更新%0.00f秒", time];
}

#pragma mark - MPBidTableViewContainerDelegate
- (void)pullRefreshDidStartWithViewController:(MPBaseViewController *)vc
{
    [self.timerLabel reset];
}

-(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime
{
    KDTableViewController* vc = (KDTableViewController *)self.selectedController;
    [vc refreshModel];
    [timerLabel start];
}

@end
